package com.ly.web.controller.king;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.heart.domain.KingBaseMeteringUnitEntity;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.service.IKingBaseMeteringUnitService;
import com.ly.heart.service.IKingOrgService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.heart.util.KingCommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api (value = "MeteringUnit", tags = "推基础数据-计量单位")
@RestController
@RequestMapping ("/heart/MeteringUnitRemote")
@Slf4j
public class MeteringUnitRemote {

    @Resource
    private IKingBaseMeteringUnitService iKingBaseMeteringUnitService;
    @Resource
    private IKingOrgService iKingOrgService;


    @ApiOperation ("同步拉取金蝶计量单位")
    @RequestMapping ("pull")
    public AjaxResult queryList () {
        KingOrgEntity orgEntity = iKingOrgService.getOne ( new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getOrgId, CurrentUserUtil.getCompanyId () ).eq ( KingOrgEntity :: getIsDisabled, false ) );
        if ( orgEntity == null ) {
            throw new CustomException ( "请先至系统管理中配置金蝶账套！" );
        }
        IdentifyInfo identifyInfo = KingCommonUtil.getIdentifyInfo ( orgEntity );
        K3CloudApi client = new K3CloudApi ( identifyInfo );
        //请求参数，要求为json字符串
        String jsonData = "{\"FormId\":\"" + KingConstUtil.BD_UNIT + "\",\"FieldKeys\":\"FNumber,FName,FDescription,FUnitGroupId.FNumber,FIsBaseUnit,FPrecision,FRoundType\",\"FilterString\":[],\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery ( jsonData );
            String s = client.executeBillQueryJson ( jsonData );
            System.out.println (s);
            for ( int i = 0; i < lists.size (); i++ ) {
                List<Object> objects = lists.get ( i );
                //根据code查询是否已存在该结算方式
                String bizCode = (String) objects.get ( 0 );
                KingBaseMeteringUnitEntity meteringUnitEntity = iKingBaseMeteringUnitService.getBaseMapper ().selectOne ( new QueryWrapper<KingBaseMeteringUnitEntity> ().eq ( "metering_unit_code", bizCode ) );
                if ( meteringUnitEntity == null ) {
                    meteringUnitEntity = new KingBaseMeteringUnitEntity ();
                    meteringUnitEntity.setMeteringUnitCode ( (String) objects.get ( 0 ) );
                    meteringUnitEntity.setMeteringUnitName ( (String) objects.get ( 1 ) );
//                    meteringUnitEntity.setExchangeDenominator ( String.valueOf ( objects.get ( 2 ) ) );//换算分母
//                    meteringUnitEntity.setExchangeNumerator ( String.valueOf ( objects.get ( 3 ) ) );//换算分子
                    meteringUnitEntity.setMeteringUnitDescribe ( String.valueOf ( objects.get ( 2 ) ) );
                    meteringUnitEntity.setMeteringUnitGroupCode ( String.valueOf ( objects.get ( 3 ) ) );
                    meteringUnitEntity.setIsBase ( (Boolean) ( objects.get ( 4 ) ) );//是否基准单位
                    meteringUnitEntity.setDefaultDegree ( String.valueOf ( objects.get ( 5 ) ) );//精度
                    meteringUnitEntity.setRoundingType ( String.valueOf ( objects.get ( 6 ) ) );//设人类型
                    meteringUnitEntity.setIsSend ( KingConstUtil.KING_DATA_IS_SEND_YES );// 表示和金蝶已同步
                    iKingBaseMeteringUnitService.insertKingBaseMeteringUnitEntity ( meteringUnitEntity );
                }
            }
        } catch ( Exception e ) {
            e.printStackTrace ();
            throw new RuntimeException ( "同步计量单位失败" );
        }
        return AjaxResult.success ( "同步计量单位成功" );
    }


}
